var app = angular.module('ZJPC');
app.controller('CarListController', ['$scope','$state','$rootScope','DataService',CarListController]);

function CarListController ($scope,$state,$rootScope,DataService) {
	
	$scope.clearInput = function(e){
		$scope.search_entry = '';
	}

	$scope.searchList = []

	$scope.pageOption = {
		pageSize:10,
		itemCount:0,
		current:0
	}
	var carlist = []

	function callback(data){
		/*carlist = data;*/

	}

	$scope.getData = function(data){
		$scope.loading = true;
		$scope.searchTrack();
	}

	$scope.setData = function(data){
		$scope.pageOption.current = 0;
		$scope.loading = false;
		$scope.pageOption.itemCount = data.total;
		$scope.searchList  = data.data;
	}



	$scope.clickDetail = function(name,li){
		$scope.$emit('detail',{name:name,type:'car-list',li:li})
/*		$state.go('app.region.main.search.cardetail.track',{name:name})*/
	}

	$scope.searchTrack = function(){
		$scope.loading = true;
		DataService.getTimeCar($scope.starttime,$scope.endtime,$scope.pageOption,$scope.setData)
	}

	$scope.endtime = new Date().setHours(23,59,59);
	$scope.starttime = new Date('2017-12-1').setHours(0,0,0);
	$scope.searchTrack();

		/*时间控件校验相关设置*/
	$scope.endDateBeforeRender = endDateBeforeRender
	$scope.endDateOnSetTime = endDateOnSetTime
	$scope.startDateBeforeRender = startDateBeforeRender
	$scope.startDateOnSetTime = startDateOnSetTime

	function startDateOnSetTime () {
	  $scope.$broadcast('start-date-changed');
	}

	function endDateOnSetTime () {
	  $scope.$broadcast('end-date-changed');
	}

	function startDateBeforeRender ($dates) {
	  if ($scope.endtime) {
	    $scope.endtime = new Date($scope.endtime).setHours(23,59,59)
	    var activeDate = moment($scope.endtime);

	    $dates.filter(function (date) {
	      return date.localDateValue() >= activeDate.valueOf()
	    }).forEach(function (date) {
	      date.selectable = false;
	    })
	  }
	}

	function endDateBeforeRender ($view, $dates) {
	  if ($scope.starttime) {
	    var activeDate = moment($scope.starttime).subtract(1, $view).add(1, 'minute');

	    $dates.filter(function (date) {
	      return date.localDateValue() <= activeDate.valueOf()
	    }).forEach(function (date) {
	      date.selectable = false;
	    })
	  }
	}
	
}